package flc.ast.fragment.date;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import flc.ast.R;
import flc.ast.databinding.FragmentDateIntervalBinding;
import flc.ast.dialog.SelDateDialog;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DateIntervalFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DateIntervalFragment extends BaseNoModelFragment<FragmentDateIntervalBinding> {

    private Date startTime, endTime;
    private SelDateDialog startDateDialog, endDateDialog;


    @Override
    protected int onCreate() {
        return R.layout.fragment_date_interval;
    }

    @Override
    protected void initView() {
        mDataBinding.llStartDate.setOnClickListener(this);
        mDataBinding.llEndDate.setOnClickListener(this);
        mDataBinding.tvStartCal.setOnClickListener(this);

        Date currentDate = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String currentTime = dateFormat.format(currentDate);
        try {
            startTime = dateFormat.parse(currentTime);
            endTime = dateFormat.parse(currentTime);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        mDataBinding.tvStartDate.setText(currentTime);
        mDataBinding.tvEndDate.setText(currentTime);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llStartDate://开始日期
                showStartDateDialog();
                break;
            case R.id.llEndDate://结束日期
                showEndDateDialog();
                break;
            case R.id.tvStartCal://开始推算
                calculate();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void calculate() {
        //毫秒ms
        long diff = endTime.getTime() - startTime.getTime();

        long diffDays = diff / (24 * 60 * 60 * 1000);

        long diffWeeks = diff / (24 * 60 * 60 * 1000 * 7);
        long diffWeekDays = diff / (24 * 60 * 60 * 1000) % 7;

        long diffMonths = diff / 24 / 60 / 60 / 1000 / 30;
        long diffMonthDays = diff / (24 * 60 * 60 * 1000) % 30;

        long diffYears = diff / 24 / 60 / 60 / 1000 / 365;
        long diffYearMonths = diff / (24 * 60 * 60 * 1000) % 365 / 30;
        long diffYearMonthDays = diff / (24 * 60 * 60 * 1000) % 365 % 30;

        long diffHours = diff / (60 * 60 * 1000);
        long diffHourMinutes = diff / (60 * 1000) % 60;

        long diffMinutes = diff / (60 * 1000);

        mDataBinding.tvDay.setText(diffDays + getString(R.string.unit_day));
        mDataBinding.tvWeek.setText(diffWeeks + getString(R.string.week_text) + diffWeekDays + getString(R.string.unit_day));
        mDataBinding.tvMonth.setText(diffMonths + getString(R.string.unit_month) + diffMonthDays + getString(R.string.unit_day));
        mDataBinding.tvYear.setText(diffYears + getString(R.string.unit_year) +
                diffYearMonths + getString(R.string.unit_month)
                + diffYearMonthDays + getString(R.string.unit_day));
        mDataBinding.tvHour.setText(diffHours + getString(R.string.unit_hour) + diffHourMinutes + getString(R.string.unit_minute));
        mDataBinding.tvMinute.setText(diffMinutes + getString(R.string.unit_minute));

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        int workdays = 0;
        while (calendar.getTime().getTime() <= endTime.getTime()) {
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            // 如果是周末，则跳过
            if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
                calendar.add(Calendar.DATE, 1);
                continue;
            }
            workdays++;
            calendar.add(Calendar.DATE, 1);
        }
        mDataBinding.tvWorkDay.setText((workdays - 1) + getString(R.string.unit_day));
    }

    //选择开始日期弹框
    private void showStartDateDialog() {
        if (startDateDialog == null) {
            startDateDialog = new SelDateDialog(mContext);
        }
        startDateDialog.title = getString(R.string.start_date);
        startDateDialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void selDate(int year, int month, int day) {
                String selDate = year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
                SimpleDateFormat stringFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date date = stringFormat.parse(selDate);
                    startTime = date;
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                mDataBinding.tvStartDate.setText(selDate);
            }
        });
        startDateDialog.show();
    }

    //选择结束日期弹框
    private void showEndDateDialog() {
        if (endDateDialog == null) {
            endDateDialog = new SelDateDialog(mContext);
        }
        endDateDialog.title = getString(R.string.end_date);
        endDateDialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void selDate(int year, int month, int day) {
                String selDate = year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
                SimpleDateFormat stringFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date date = stringFormat.parse(selDate);
                    endTime = date;
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                mDataBinding.tvEndDate.setText(selDate);
            }
        });
        endDateDialog.show();
    }

}